//
//  YishuVC.m
//  New
//
//  Created by 才诗琦 on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "YishuVC.h"

@interface YishuVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, assign)NSInteger number;
@end

@implementation YishuVC
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //创建tableView
        [self createTableView];
        //        self.number = 0;
        
    }    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
#warning 上拉加载
- (void)loadnewdata
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1441074311424/%ld-20.html", self.number]  completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [YishuModel modelHandlerWithArray:result[@"T1441074311424"]];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    [self.tableView.mj_footer endRefreshing];
    self.number += 20;
}

#warning 下拉刷新
- (void)dadaHandeler
{
    [AFNModel GETWithHandler: @"http://c.m.163.com/nc/article/list/T1441074311424/0-20.html"  completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [YishuModel modelHandlerWithArray:result[@"T1441074311424"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [self.tableView reloadData];
            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.tableView.mj_header endRefreshing];
        });
        
    }];
    
}

#warning 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-150 ) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dadaHandeler)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [self.tableView.mj_footer beginRefreshing];
    }
    self.number = 20;
    
    //注册cell
    //页面首条数据
    [_tableView registerClass:[YishuCell class] forCellReuseIdentifier:@"cell"];
    
    //一张图片的cell
    [_tableView registerClass:[YishuCell2 class] forCellReuseIdentifier:@"cell2"];
    //三张图片的cell
    [_tableView registerClass:[YishuCell3 class] forCellReuseIdentifier:@"cell3"];
    
    
    //添加父视图
    [self.view addSubview:_tableView];
    //管理内存
    [_tableView release];
    
    
}

#warning 处理数据 方法实现
- (UITableView *)setWithUrlStr:(NSString *)str
{
    //
    [AFNModel GETWithHandler:str completion:^(id result) {
        
        self.dataArr = [YishuModel modelHandlerWithArray:result[@"T1441074311424"]];
        
        [_tableView reloadData];
        
        
    }];
    
    
    
    return _tableView;
}
//tableView的协议方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YishuModel *yule =_dataArr[indexPath.row];
    //判断
    if ([yule.hasHead stringValue].length != 0) {
        
        //页面第一条 为第一个cell
        YishuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.imagView sd_setImageWithURL:[NSURL URLWithString:yule.imgsrc]];
        cell.titleLabel.text = yule.title;
        
        return cell;
        
    }
    //显示三张图片的cell 为第二个cell
    if (yule.imgextra != NULL  && [yule.hasHead stringValue].length == 0) {
        
        YishuCell3 *cell3 = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        [cell3.imagView sd_setImageWithURL:[NSURL URLWithString:yule.imgextra[0][@"imgsrc"]]];
        [cell3.imagView1 sd_setImageWithURL:[NSURL URLWithString:yule.imgextra[1][@"imgsrc"]]];
        
        [cell3.imagView2 sd_setImageWithURL:[NSURL URLWithString:yule.imgsrc]];
        cell3.titleLabel.text = yule.title;
        return cell3;
    }
    
    //    显示一张图片 的cell 为第三个cell
    else{
        YishuCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
        cell2.titleLabel.text = yule.title;
        cell2.contentLabel.text = yule.digest;
        [cell2.imag sd_setImageWithURL:[NSURL URLWithString:yule.imgsrc]];
        cell2.commentCountLabel.text = [yule.replyCount stringValue];
        return cell2;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YishuModel *yule =_dataArr[indexPath.row];
    
    if ([yule.digest isEqualToString:@""]) {
        
        
        //cell1的高度
        if ([yule.hasHead stringValue].length == 0) {
            
            return 200;
        }
        //cell的高度
        return 170;
    }
    else
    {
        //cell2的高度
        CGFloat h = [YishuCell2 heightWithText:yule.digest];
        return h + 100;
        
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YishuModel *yishu = _dataArr[indexPath.row];
    [self.delegate passValueYishu:yishu.url];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
